import { systemUpgradeDetail, saveServ, deleteServ } from './updateDescSettingListServ'

import { parse } from 'qs'

import { message } from 'antd'

import moment from 'moment'

import { isEmpty, isArray, cloneDeep, merge } from 'lodash'

import { hashHistory } from 'dva/router'

const State = {
    biz: {
        queryForm: {}
    },
    actionType: 'add',
    detail: {},
    visible: false,
    detailVisible: false,
    pageProps1: {}
}

// 声明module
const tmpModule = {
    state: cloneDeep(State),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                    // dispatch({type: 'queryInfo', payload: {}}) // end dispatch
                }
            })
        },
    },

    // 方法列表
    effects: {
        * setFormVal({ payload }, { put, call, select }) {
            let { biz } = yield select(d => d[tmpModule.namespace])
            try {
                yield put({ type: 'updateStore', payload: { biz: payload } })
            } catch (e) {
                console.error(e)
            }
        },
        * systemUpgradeDetail({ payload }, { put, call, select }) {
            const { data, success } = yield call(systemUpgradeDetail, parse({ contentId: payload.contentId }))
            if (success) {
                yield put({ type: 'updateStore', payload: { detail: data, detailVisible: true } })
            }
        },
        * systemUpgradeSave({ payload }, { put, call, select }) {
            let { detail, biz } = yield select(d => d[tmpModule.namespace])
            detail.published = 1
            detail.endDate = detail.startDate
            try {
                const { data, success } = yield call(saveServ, parse({...detail }))
                if (success) {
                    yield put({ type: 'updateStore', payload: { visible: false } })
                    biz.refreshListDatas(true)
                }
            } catch (e) {
                console.error(e)
            }
        },
        * deleteSystemUpdate({ payload }, { put, call, select }) {
            let { biz } = yield select(d => d[tmpModule.namespace])
            try {
                const { success } = yield call(deleteServ, parse({ contentId: payload.contentId }))
                if (success) {
                    biz.refreshListDatas(true)
                }
            } catch (e) {
                console.error(e)
            }
        },


        // // 删除标签
        // *labelDel({ payload }, { put, call, select }) {
        //     let {biz} = yield select(d => d[tmpModule.namespace])
        //     try{
        //       const { data , success } = yield call(labelDelete, parse({ tagId:payload.tagId }))
        //         if(success){
        //           biz.refreshListDatas(true)

        //         }
        //     }catch(e){
        //         console.error(e)
        //     }
        // },
        // // 新增标签
        // *labelAdd({ payload }, { put, call, select }) {
        //     let {biz,detailData} = yield select(d => d[tmpModule.namespace])
        //     try{
        //         const { data , success,pageNo } = yield call(labelCreate, parse({
        //             name:detailData.labelList,
        //             type:detailData.type
        //         }))
        //         if(success){
        //             biz.refreshListDatas()
        //             yield put({type: 'updateStore', payload: { showTabelModal: false,
        //                     detailData:{
        //                         labelList:[],
        //                         type:'',
        //                         name:"",
        //                         tagId:""
        //                     }}})
        //         }
        //     }catch(e){
        //         console.error(e)
        //     }
        // },
        // // 标签标签
        // *update({ payload }, { put, call, select }) {
        //     let {biz,detailData} = yield select(d => d[tmpModule.namespace])
        //     try{
        //         const { data , success } = yield call(labelUpdate, parse({
        //             name:detailData.name,
        //             type:detailData.type,
        //             tagId:detailData.tagId,
        //         }))
        //         if(success){
        //             biz.refreshListDatas(true)
        //             yield put({type: 'updateStore', payload: { showTabelModal: false}})
        //         }
        //     }catch(e){
        //         console.error(e)
        //     }
        // },



        * updateModel({ payload }, { put, select }) {
            yield put({ type: 'updateStore', payload });
        },
    },

    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return Object.assign({}, preState, action.payload)
        },
    }
}

export default tmpModule